﻿(function ($) {
    var methods = {
        init: function (options) {
            return this.each(function () {
                var settings = {
                    onButtonClick: null
                };
                var $this = $(this),
                    data = $this.data('valueSelector'),
                    valueSelector = $('<div />', {
                });

                // If the plugin hasn't been initialized yet
                if (!data) {

                    //                    $(this).html(
                    //                    '<div class="valueSelector">' +
                    //                        '<input readOnly="readOnly" type="text" meta="jControl" class="valueSelectorContent" />' +
                    //                        '<input class="selector ui-state-default ui-corner-all" type="button" value="..." />' +
                    //                    '</div>'
                    //                );
                    if (options) {
                        $.extend(settings, options);
                    }
                    //data object ---------------------------------------------------------
                    $(this).data('valueSelector', {
                        target: $this,
                        valueSelector: valueSelector,
                        settings: settings,
                        Content: $(this).find(".valueSelectorContent"),
                        BTNSelect: $(this).find(".valueSelectorButton"),
                        Values: {}
                    });

                    //Local Objects ------------------------------------------------------
                    var Content = $(this).data('valueSelector').Content;
                    var BTNSelect = $(this).data('valueSelector').BTNSelect;

                    //Initial Configuration -----------------------------------------------
                    var o = settings;
                    //                    if (o.onButtonClick === 'function') {
                    //                        BTNSelect.bind("click.valueSelector", { context: $(this) }, function (event) {
                    //                            o.onButtonClick();
                    //                        });
                    //                    }

                    _ensureDimension($this);
                    _ensureItemsIternal($this);
                };
            });
        },

        destroy: function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('valueSelector');

                $(window).unbind('.valueSelector');
                if (data) {
                    data.valueSelector.remove();
                    $this.removeData('valueSelector');
                }
            })
        },

        addItem: function (text, value) {
            return this.each(function () {
                _addItemIternal($(this), text, value);
            })
        },

        clear: function () {
            return this.each(function () {
                _clearListIternal($(this), true);
            })
        }
    };

    $.fn.valueSelector = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.valueSelector');
        }
    };

    function _ensureItemsIternal($context) {
        _clearListIternal($context, false);
        var $this = $context, data = $this.data('valueSelector');
        var value = $context.attr('jValue');
        if (value !== '') {
            var valueList = $.evalJSON(value);
            $.each(valueList, function (key, value) {
                _addItemIternal($context, value, key);
            });
        }
    };

    function _clearListIternal($context, clearJValue) {
        var $this = $context, data = $this.data('valueSelector');
        data.Values = {};
        $this.val('');
        data.Content.val('');
        if (clearJValue === true) {
            $this.attr('jValue', '');
        }
    };

    function _ensureDimension($context) {
        var $this = $context, data = $this.data('valueSelector');
        data.Content.css({
            width: $context.innerWidth() - (3 * data.BTNSelect.outerWidth()) - 3 + 'px'
        });
        data.BTNSelect.css({
            height: data.Content.outerHeight() - 1 + 'px'
        });
    };

    function _addItemIternal($context, text, value) {
        var $this = $context, data = $this.data('valueSelector');

        var valueList = data.Values;
        valueList[value] = text;

        var text = '';
        $.each(valueList, function (key, value) {
            text += value + '; ';
        });
        data.Content.val(text);
        $context.val($.toJSON(valueList));
    };

})(jQuery);